<?php 
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Loader;
use app\index\model\Notice as NoticeModel;


class Valid extends Controller {

    private $data = [
        // 'title' => '环形手表⌚️',
        // 'content' => '大块手表⌚⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️⌚️️',
        'nt_id' => 3,
        // 'status' => 'on'
    ];

    public function index() {
        "https://www.kancloud.cn/manual/thinkphp5/129322";

        // 方法一
        $validate = new Validate([
            'title' => 'require|max:25', // ['require','max'=>25]
            'content' => 'require|max:500' // ['require', 'max'=>'500']
        ]); // 第二个参数为错误返回文字

        // 方法二
        $validate->rule('nt_id','require'); // 也可以使用数组多个定义

        if(!$validate->check($this->data)) {
            return $validate->getError();
        }

        return json($this->data);
    }

    public function validClass() {

        // 方法三
        $validate = Loader::validate('Notice');

        $validate->check($this->data);

        var_dump($validate->getError());

        $validate->batch()->check($this->data);
        var_dump($validate->getError());

    }

    public function validCallback() {
        $validate = new Validate([
            'status' => function($value, $data) {
                return $value == 'on' || $value == 'off';
            }
        ], [
            'status' => '状态值错误' // 回调方式不能改错误信息？
        ]);
        $validate->check($this->data);
        var_dump($validate->getError());
    }

    public function extendFunction() {
        $validate = new Validate();
        $validate->extend([
            'checkStatus' => function($value, $rule, $data, $key, $desc) {
                return $value == 'on' || $value == 'off';
            }
        ]);
    }

    public function scene() {

        $validate = Loader::validate('Notice');

        $validate->scene('edit')->check($this->data);

        var_dump($validate->getError());

    }

    public function controllervalid() {
        // $res = $this->validate($this->data, ['title'=>'require']);
        // $res = $this->validate($this->data, 'Notice'); // 返回true或失败信息
        $res = $this->validate($this->data, 'Notice.edit'); // 使用场景
        var_dump($res);
    }

    public function modelvalid() {

        $notice = new NoticeModel;

        // $notice->validate(true)->save($this->data);
        $notice->validate('Notice.edit')->save($this->data);

        var_dump($notice->getError());

    }

    public function staticvalid() {

        // 数据均在第一个参数，规则均在第二个参数
        // 通过返回true，否则返回false
        Validate::dateFormat('2016-03-08', $data);
        Validate::is();
        Validate::in();
        Validate::gt();
        Validate::regex();

    }

    public function tokenForm() {
        return $this->fetch('index');
    }

    public function tokenvalie() {
        $valid = Loader::validate('Notice');
        $res = $valid->scene('edit')->check($_POST);

        var_dump($_POST);

        var_dump($res, $valid->getError());
    }

}